/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.parser;

import java.util.List;

import dex.compiler.model.base.Place;
import dex.compiler.model.expression.LiteralFunction;
import dex.compiler.model.expression.LiteralInteger;
import dex.compiler.model.expression.LiteralString;
import dex.compiler.model.type.TypeNode;


/**
 * Parser for literal strings, numbers and functions.
 */
class LiteralParser extends TypeParser {
	
	
	/**
	 * Constructs a new LiteralParser.
	 * 
	 * @param config  the configuration for the new parser.
	 */
	public LiteralParser(ParserConfig config) {
		super(config);
	}
	
	
	/**
	 * Parses a literal string.
	 * 
	 * @return the literal string that was parsed
	 */
	public LiteralString parseLiteralString() {
		char ch = input.skipAndRead();
		if (ch != '\"') {
			raise("Expected literal string.");
		}
		StringBuilder result = new StringBuilder();
		for (ch = input.read(); (ch != '\"'); ch = input.read()) {
			if (ch == 0) {
				raise("Unterminated string literal.");
			}
			if (ch == '\\') {
				char escape = input.read();
				switch (escape) {
					case 'n':
						result.append('\n');
						break;
					case 'r':
						result.append('\r');
						break;
					case 't':
						result.append('\t');
						break;
					case '\\':
						result.append('\\');
						break;
					case '\"':
						result.append('\"');
						break;
					case 'u':
						result.append(readHexUnicode());
					default:
						raise("Illegal escape character: " + escape);
				}
			} else {
				result.append(ch);
			}
		}
		return new LiteralString(input.getPlace(), result.toString());
	}
	
	
	/**
	 * Reads four hex digits and returns the unicode character they
	 * form.
	 * 
	 * @return  the unicode character
	 */
	private char readHexUnicode() {
		StringBuilder sb = new StringBuilder(4);
		for (int i = 0; i < 4; i++) {
			sb.append(input.read());
		}
		return (char)(Integer.parseInt(sb.toString(), 16));
	}

	
	/**
	 * Parses a literal integer.
	 * 
	 * @return  the parsed literal integer 
	 */ 
	public LiteralInteger parseLiteralInteger() {
		// FIXME: Support hex, octal
		StringBuilder digits = new StringBuilder();
		char ch = input.skipAndPeek();
		if (ch == '-') {
			digits.append(ch);
			ch = input.peek();
		}
		if (!Character.isDigit(ch)) {
			raise("Expected literal integer but got " + (int)ch);
		}
		while (Character.isDigit(ch)) {
			input.expect(ch);
			digits.append(ch);
			ch = input.peek();
		}
		long v = Long.parseLong(digits.toString());
		return new LiteralInteger(input.getPlace(), v);
	}


	/**
	 * Parses a function literal.
	 * 
	 * @return  the parsed function literal.
	 */
	public LiteralFunction parseFunctionLiteral() {
		Place place = input.getPlace();
		input.skipAndExpect('$'); // ew
		String id = parseIdentifier();
		List<TypeNode> types = parseTypeList();
		return new LiteralFunction(place, id, types);
	}


}
